#include "additemdialog.h"
#include "ui_additemdialog.h"

#include <QRadioButton>
#include <QMetaType>
#include <QSpacerItem>
#include <QLabel>
#include <QPushButton>


AddItemDialog::AddItemDialog(const QList<int> &types, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddItemDialog)
{
    m_buttons = new QButtonGroup(this);
    ui->setupUi(this);

    QWidget* prev = ui->lineEdit;
    foreach (int type, types)
    {

        QString text = QMetaType::typeName(type);
        QRadioButton* radio = new QRadioButton(text, this);
        m_buttons->addButton(radio, type);
        ui->verticalLayout->addWidget(radio);
        QWidget::setTabOrder(prev, radio);
        prev = radio;
    }


    if (!m_buttons->buttons().isEmpty())
    {
        m_buttons->buttons().first()->setChecked(true);
        ui->labelError->hide();
    }

    m_buttons->setExclusive(true);

    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->labelError->isVisible());

    ui->verticalLayout->addSpacerItem(new QSpacerItem(1,1, QSizePolicy::Preferred, QSizePolicy::Expanding));

    QWidget::setTabOrder(prev, ui->buttonBox);
    ui->label->hide();
    ui->lineEdit->hide();
}

AddItemDialog::~AddItemDialog()
{
    delete ui;
}

QString AddItemDialog::name() const
{
    return ui->lineEdit->text();
}

int AddItemDialog::type() const
{
    foreach (QAbstractButton* button, m_buttons->buttons())
    {
        if (button->isChecked())
        {
            return m_buttons->id(button);
        }
    }

    return -1;
}


